package cn.edu.swu.bc.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthFilter extends HttpFilter {

    private static final long serialVersionUID = -2840770222442338931L;
    public static String LOGIN_STATUS = "LOGIN_STATUS";//用户状态
    public static String IS_ADMIN = "IS_ADMIN";//是否为管理员
    public static String LOGIN_VALIDATE_CODE = "LOGIN_VALIDATE_CODE";//验证码
    public static String USER_ID ="USER_ID";//用户id
    public static String USER_NICKNAME ="USER_NICKNAME";//用户昵称
    public static String USER_AVATAR ="USER_AVATAR";//用户头像
    public static String USER_SIGNATURE ="USER_SIGNATURE";//用户签名

    public void doFilter(HttpServletRequest request,
                         HttpServletResponse response, FilterChain chain)
            throws java.io.IOException, ServletException {
        HttpSession session = request.getSession(true);
        Boolean status = (Boolean) session.getAttribute(LOGIN_STATUS);//获取用户状态
        if (status == null || status.equals(Boolean.FALSE)) {//若未登录则跳转到登录页面
            response.sendRedirect("/MySpace/login.jsp");
        } else {
            chain.doFilter(request, response);
        }
    }
}
